package seismosurfer.domain;

import java.io.Serializable;
import java.security.Principal;
import java.util.Collection;

import seismosurfer.data.UserData;

/**
 * Principal - a simple, serializable Principal. Based 
 * on the securityfilter homonym class.
 */
public class SimplePrincipal implements Principal, Serializable {
    
    private static final long serialVersionUID = 1746507214786950521L;

    private String _name;

    private Collection _roles;

    private UserData _user;

    public SimplePrincipal(String name, Collection roles, UserData user) {
        _name = name;
        _roles = roles;
        _user = user;
    }

    /**
     * Returns the name of this principal.
     * 
     * @return the name of this principal.
     */
    public String getName() {
        return _name;
    }

    public UserData getUser() {
        return _user;
    }

    public boolean isInRole(String rolename) {
        return (_roles != null && _roles.contains(rolename));
    }

    /**
     * Compares this principal to the specified object.
     * 
     * @param o    object to compare with.
     * 
     * @return true if the object passed in is a SimplePrincipal with the same
     *         name.
     */
    public boolean equals(Object o) {
        return (o instanceof Principal)
                && _name.equals(((Principal) o).getName());
    }

    /**
     * Returns a string representation of this principal.
     * 
     * @return a string representation of this principal.
     */
    public String toString() {
        return "Principal[name = \'" + _name + "\']";
    }

    /**
     * Returns a hashcode for this principal.
     * 
     * @return a hashcode for this principal.
     */
    public int hashCode() {
        return _name.hashCode();
    }
}